package com.itdlc.android.library.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.itdlc.android.library.utils.ToastUtil;

import org.simple.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {
    protected String TAG = this.getClass().getSimpleName();
    private Unbinder mUnbinder;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        initReceiver();
        setContentView(getLayoutRes());
        mUnbinder = ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initialView();
        initData();

    }

    protected void initData() {

    }

    /**
     * 网络监听
     */
    private void initReceiver() {

    }


    protected void initialView() {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mUnbinder.unbind();
    }


    /**
     * 获取布局ID
     */
    protected abstract int getLayoutRes();




    public void showShortToast(int strId) {
//        ToastUtil.showOne(this, getString(strId), Toast.LENGTH_SHORT);
    }


    public void showShortToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
//        ToastUtil.showOne(this, str, Toast.LENGTH_SHORT);
    }


    public void showLongToast(int strId) {
//        ToastUtil.showOne(this, getString(strId), Toast.LENGTH_LONG);
    }


    public void showLongToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
//        ToastUtil.showOne(this, str, Toast.LENGTH_LONG);
    }

    /**
     * startActivity with bundle
     */
    public void readyGo(Class<?> clazz, Bundle bundle) {
        if (clazz != null) {
            Intent intent = new Intent(this, clazz);
            if (null != bundle) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
        }
    }

    /**
     * startActivity
     */
    public void readyGo(Class<?> clazz) {
        readyGo(clazz, null);
    }

    public void readyGo(Class<?> clazz, int requestCode) {
        readyGo(clazz, null, requestCode);
    }

    public void readyGo(Class<?> clazz, Bundle bundle, int requestCode) {
        if (clazz != null) {
            Intent intent = new Intent(this, clazz);
            if (null != bundle) {
                intent.putExtras(bundle);
            }
            if (requestCode == 0) {
                startActivity(intent);
            } else {
                startActivityForResult(intent, requestCode);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            onActivityResult(requestCode, data);
        }
    }

    protected void onActivityResult(int requestCode, Intent data) {

    }

    /**
     * startActivity with bundle and finish
     */
    public void readyGoThenKill(Class<?> clazz, Bundle bundle) {
        if (clazz != null) {
            Intent intent = new Intent(this, clazz);
            if (null != bundle) {
                intent.putExtras(bundle);
            }
            startActivity(intent);
            finish();
        }

    }

    /**
     * startActivity and finish
     */
    public void readyGoThenKill(Class<?> clazz) {
        readyGoThenKill(clazz, null);
    }


    /**
     * 点击空白区域隐藏键盘.
     *
     * @param event the event
     * @return true, if successful
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null) {
                if (getCurrentFocus().getWindowToken() != null) {
                    imm.hideSoftInputFromWindow(getCurrentFocus()
                            .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
        return super.onTouchEvent(event);
    }



}

